﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Windows;
using Passwords.Controls;
using Passwords.Controllers;
using Passwords.Models;
using Passwords.Utils;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using Silvermoon.Images;
using Passwords.Images;
using Passwords.Styles;
using Silvermoon.Windows.Styles;

namespace Passwords.Pages
{
    public class PasswordsTab : TabControl
    {
        private static int TabIndex = 0;
        private TabPage allPasswordsTab;
        private TabPage categoriesTab;
        private TabPage favesTab;

        public PasswordsTab()
            : base()
        {
            Initialize();
        }

        public ListView AllListView { get; private set; }
        public ListView CategoriesListView { get; private set; }
        public ListView FavesListView { get; private set; }

        private void Initialize()
        {
            int tabIndex = PasswordsTab.TabIndex;

            Styles.SetNamed(new LabelStyle { Name = TabPage.LabelStyleName, Font = FontCache.BigRegularFont, FontScale = 24f / 16f, AutoSize = true });

            TabBackground = PhoneColors.Black;

            AllListView = new ListView { Margin = Margin.Zero, AlwaysShowGroupHeader = true };
            CategoriesListView = new ListView { Margin = Margin.Zero };
            FavesListView = new ListView { Margin = Margin.Zero };

            CategoriesListView.Styles.SetNamed(new Style { Name = "GroupButton", StateManager = ButtonStateManagers.ButtonManager });

            allPasswordsTab = new TabPage("all", AllListView);
            categoriesTab = new TabPage("categories", CategoriesListView);
            favesTab = new TabPage("favorites", FavesListView);
            SetPages(allPasswordsTab, categoriesTab, favesTab);
            Margin = Margin.Zero;

            SelectedPage = GetPageFromIndex(tabIndex);

        }


        private TabPage GetPageFromIndex(int tabIndex)
        {
            switch (tabIndex)
            {
                case 0: return allPasswordsTab;
                case 1: return categoriesTab;
                case 2: return favesTab;
                default: return allPasswordsTab;
            }
        }


        private string GetTitleLabel(string title)
        {
            return title;
        }

        protected override void OnPageChanged(object sender, TabPage page)
        {
            base.OnPageChanged(sender, page);
            if (page != null) TabIndex = GetTabIndexFromPage(page);
        }

        private int GetTabIndexFromPage(TabPage page)
        {
            if (page == allPasswordsTab) return 0;
            else if (page == categoriesTab) return 1;
            else if (page == favesTab) return 2;
            return -1;
        }

        internal void GotoSearchPage()
        {
            SetPage(allPasswordsTab, false);
        }
    }
}
